package testplan.servlets;

import java.io.IOException;
import java.util.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import testplan.entities.Project;
import testplan.entities.Release;
import testplan.entities.TestPlan;
import testplan.entities.User;
import testplan.services.ProjectService;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import testplan.services.TestPlanService;

@Singleton
public class ProjectServlet extends HttpServlet 
{
	@Inject
	private ProjectService service;

    @Inject
    private TestPlanService testPlanService;
	
	protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
		checkNotNull(service);
        final String action = request.getParameter("action");
        try
        {
	        if("create".equals(action))
	        {
	        	doCreate(request, response);
	        }
	        else if("list".equals(action))
	        {
	        	doList(request, response);
	        }
        }
        catch(JSONException e)
        {
        	throw new RuntimeException(e);
        }
    }
	
	// create project
	private void doCreate(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		final String projectName = request.getParameter("name");
		final String description = request.getParameter("desc");
		final User user = (User) request.getSession().getAttribute("user");
		checkNotNull(projectName);
		checkNotNull(description);
		checkNotNull(user);
		service.createProject(projectName, user.getAgency(), description);
		response.getOutputStream().println("OK");
	}
	
	private void doList(HttpServletRequest request, HttpServletResponse response) throws IOException, JSONException
	{

        final List<Map> projects = new ArrayList<Map>();

        final User user = (User) request.getSession().getAttribute("user");
        final long agencyId = user.getAgency();
		for(Project project: service.findByAgency(agencyId))
        {
            Map<String, Object> projectInfo = new HashMap<String, Object>();
            projectInfo.put("name", project.getName());
            projectInfo.put("key", project.getKey());
            projectInfo.put("description", project.getDescription());

            // test plans
            List<Map> testPlanList = new ArrayList<Map>();
            for(TestPlan plan: testPlanService.getTestPlans(project.getKey()))
            {
                Map<String, Object> testPlanInfo = new HashMap<String, Object>();
                testPlanInfo.put("name", plan.getName());
                testPlanInfo.put("key", plan.getKey());
                testPlanInfo.put("description", plan.getDescription());
                testPlanInfo.put("rate", 90); // TODO: compute this
                testPlanList.add(testPlanInfo);
            }
            projectInfo.put("plans", testPlanList);

            // releases
            List<Map> releaseList = new ArrayList<Map>();
            for (Release release : service.getReleases(project.getKey()))
            {
                Map<String, Object> releaseInfo = new HashMap<String, Object>();
                releaseInfo.put("name", release.getName());
                releaseInfo.put("key", release.getKey());
                releaseInfo.put("status", release.getStatus().name());
                releaseList.add(releaseInfo);
            }
            projectInfo.put("releases", releaseList);

            projects.add(projectInfo);
        }

        final Map<String, Object> result = new HashMap<String, Object>();
		result.put("projects", projects);
		response.getOutputStream().println(new JSONObject(result).toString(2));
	}

}
